//
//  UIDevice+SafeArea.m
//  shubao
//
//  Created by yby on 2022/9/23.
//

#import "UIDevice+SafeArea.h"
//#import <Darwin.POSIX.ifaddrs>
#import <ifaddrs.h>

@implementation UIDevice (SafeArea)

/// 获取window
+ (UIWindow *)wb_getWindow {
    UIWindow *window;
    if (@available(iOS 13.0, *)) {
        NSSet *var_set = [UIApplication sharedApplication].connectedScenes;
        UIWindowScene *var_windowScene = [var_set anyObject];
        window = var_windowScene.windows.firstObject;
    } else if (@available(iOS 11.0, *)) {
        window = [UIApplication sharedApplication].windows.firstObject;
    }
    return window;
}
/// 顶部状态栏高度
+ (CGFloat)wb_statusBarHeight {
    if (@available(iOS 13.0, *)) {
        NSSet *var_set = [UIApplication sharedApplication].connectedScenes;
        UIWindowScene *var_windowScene = [var_set anyObject];
        UIStatusBarManager *var_statusBarManager = var_windowScene.statusBarManager;
        return var_statusBarManager.statusBarFrame.size.height;
    } else {
        return [UIApplication sharedApplication].statusBarFrame.size.height;
    }
}

#pragma mark - 安全域高度
/// 顶部安全区高度   刘海屏44 直屏24
+ (CGFloat)wb_safeTop {
    if (@available(iOS 11.0, *)) {
        return [UIDevice wb_getWindow].safeAreaInsets.top;
    } else {
        return [UIDevice wb_statusBarHeight];
    }
}
/// 底部安全区高度   刘海屏34
+ (CGFloat)wb_safeBottom {
    if (@available(iOS 11.0, *)) {
        return [UIDevice wb_getWindow].safeAreaInsets.bottom;
    } else {
        return 0;
    }
}


/// 导航栏高度
+ (CGFloat)wb_navBarHeight {
    //return [UIViewController currentNavigationController].navigationBar.frame.size.height;
    return 44.0f;
}
/// 底部tabbar高度
+ (CGFloat)wb_tabBarHeight {
    return 49.0f;
}


#pragma mark ---- 计算好的，直接用
/// 屏幕高度
+ (CGFloat)wb_screenHeight {
    return [UIScreen mainScreen].bounds.size.height;
}

/// 顶部状态栏+导航栏的高度
+ (CGFloat)wb_fullNavHeight {
    return [UIDevice wb_safeTop] + [UIDevice wb_navBarHeight];
}
/// 底部tabbar+安全域高度
+ (CGFloat)wb_fullTabHeight {
    return [UIDevice wb_safeBottom] + [UIDevice wb_tabBarHeight];
}

/// 首页无navbar，有tabbar的高度
+ (CGFloat)wb_homeNonavHeight {
    return [UIDevice wb_screenHeight]-[UIDevice wb_fullTabHeight];
}

/// 首页View高度
+ (CGFloat)wb_homeHeight {
    CGFloat var_screenHeight = [UIScreen mainScreen].bounds.size.height;
    return var_screenHeight - [UIDevice wb_fullNavHeight] - [UIDevice wb_fullTabHeight];
}

/// 二级页面View高度
+ (CGFloat)wb_viewHeight {
    CGFloat var_screenHeight = [UIScreen mainScreen].bounds.size.height;
    return var_screenHeight - [UIDevice wb_fullNavHeight];
}


+ (BOOL)wb_isCharging {
    UIDevice *device = [UIDevice currentDevice];
    
    if (device.batteryState == UIDeviceBatteryStateUnplugged) {
        NSLog(@"设备不在充电");
        return NO;
    } else if (device.batteryState == UIDeviceBatteryStateCharging) {
        NSLog(@"设备正在充电");
        return YES;
    }
    return NO;
}

+ (BOOL)wb_isRoot {
    // 判断文件是否存在，root通常会有su命令或者其他工具
    NSString *path1 = @"/bin/bash";
    NSString *path2 = @"/usr/sbin/sshd";
    NSString *path3 = @"/etc/apt/sources.list";
    NSString *path4 = @"/etc/ssh/sshd_config";
    NSString *path5 = @"/usr/bin/ssh";
    NSString *path6 = @"/etc/apt/sources.list.d/debian-archive.list";
    NSString *path7 = @"/etc/apt/sources.list.d/raspbian.list";
 
    BOOL file1Exists = [[NSFileManager defaultManager] fileExistsAtPath:path1];
    BOOL file2Exists = [[NSFileManager defaultManager] fileExistsAtPath:path2];
    BOOL file3Exists = [[NSFileManager defaultManager] fileExistsAtPath:path3];
    BOOL file4Exists = [[NSFileManager defaultManager] fileExistsAtPath:path4];
    BOOL file5Exists = [[NSFileManager defaultManager] fileExistsAtPath:path5];
    BOOL file6Exists = [[NSFileManager defaultManager] fileExistsAtPath:path6];
    BOOL file7Exists = [[NSFileManager defaultManager] fileExistsAtPath:path7];
 
    return (file1Exists || file2Exists || file3Exists || file4Exists || file5Exists || file6Exists || file7Exists);
}


+ (BOOL)wb_isProxy {
    NSDictionary *proxySettings = (__bridge NSDictionary *)(CFNetworkCopySystemProxySettings());
    NSArray *proxies = (__bridge NSArray *)(CFNetworkCopyProxiesForURL((__bridge CFURLRef _Nonnull)([NSURL URLWithString:@"http://www.baidu.com"]), (__bridge CFDictionaryRef _Nonnull)(proxySettings)));
    
    NSDictionary *settings = [proxies objectAtIndex:0];
//    NSLog(@"host=%@", [settings objectForKey:(NSString *)kCFProxyHostNameKey]);
//    NSLog(@"port=%@", [settings objectForKey:(NSString *)kCFProxyPortNumberKey]);
//    NSLog(@"type=%@", [settings objectForKey:(NSString *)kCFProxyTypeKey]);
    
    if ([[settings objectForKey:(NSString *)kCFProxyTypeKey] isEqualToString:@"kCFProxyTypeNone"]) {
        //没有设置代理
        return NO;
    }else{
        //设置代理了
        return YES;
    }
}

+ (BOOL)wb_isVPNOn {
    BOOL flag = NO;
    
    struct ifaddrs *interfaces = NULL;
    struct ifaddrs *temp_addr = NULL;
    int success = 0;
    
    // retrieve the current interfaces - returns 0 on success
    success = getifaddrs(&interfaces);
    if (success == 0) {
        // Loop through linked list of interfaces
        temp_addr = interfaces;
        while (temp_addr != NULL) {
            NSString *string = [NSString stringWithFormat:@"%s" , temp_addr->ifa_name];
            if ([string rangeOfString:@"tap"].location != NSNotFound ||
                [string rangeOfString:@"tun"].location != NSNotFound ||
                [string rangeOfString:@"ipsec"].location != NSNotFound ||
                [string rangeOfString:@"ppp"].location != NSNotFound)
            {
                flag = YES;
                break;
            }
            temp_addr = temp_addr->ifa_next;
        }
    }
    // Free memory
    freeifaddrs(interfaces);
    return flag;
}

 
@end


